<%- new_repo_class = (scope[:current_repo] == nil) ? "selected" : "" %>
<%= link_to content_tag(:span, 'Add New Repository'), package_repositories_new_path(), :class => ['add ', new_repo_class] %>
<%- if scope[:package_repositories].empty? %>
    <p id="no-items">No repository found.</p>
<%- else %>

    <input type="text" class="search" placeholder='Search for Repository or Package'/>

    <p style="display: none" class="no-items">No repository found.</p>

    <ul class="repositories treenav accordion">
        <%- isCurrent = false
            scope[:package_repositories].each do |repo|
              if scope[:current_repo] == repo.getName()
                isCurrent = true
              end
        %>
            <li class="<%= isCurrent ? (scope[:package_id] ? 'grey_selected' : 'selected') : '' %>" title="<%= repo.getName() -%>">
                <span class="handle"></span>

                <div class="entity">
                    <%= link_to repo.getName(), package_repositories_edit_path(:id => repo.getId()) -%>
                    <%= form_for repo, :as => :repo, :url => package_repositories_delete_path(:id => repo.getId()), :html => {:id => "delete_repository_"+repo.getId, :method => :delete} do |f| %>
                        <% if @cruise_config.canDeletePackageRepository(repo) %>
                            <%= md5_field %>
                            <button title='Delete this repository.' type="button" class="remove" id="button_for_repository_delete_from_tree_<%= repo.getId() %>"></button>
                            <%= render :partial => 'admin/shared/submit_with_prompt.html',
                                       :locals => {:scope => {:button_container => "delete_repository_#{repo.getId()}", :button_id => "button_for_repository_delete_from_tree_#{repo.getId()}", :message => "You are about to delete repository #{repo.getName()}", :on_confirmation_js => "$('#{"delete_repository_#{repo.getId()}"}').submit()"}} %>
                        <% else %>
                            <button type="button" title="One or more packages in this repository are being used by pipeline(s). Cannot delete repository." class="remove" id="delete_repository_button_<%= repo.getId() %>" disabled="disabled"></button>
                        <% end %>
                    <% end %>
                </div>

                <ul class="packages">
                    <%- if repo.getPackages().empty? %>
                        <li class="empty-node"><span>No packages found</span></li>
                    <%- else %>
                        <% repo.getPackages().each do |pkg| %>
                            <li title="<%= pkg.getName() -%>" class="<%= pkg.getId() == scope[:package_id] ? 'selected' : '' %>">
                                <div class="entity">
                                    <%= link_to pkg.getName(), package_definitions_show_with_repository_list_path(:repo_id => repo.getId(), :package_id => pkg.getId()) -%>
                                    <%= form_for pkg, :as => :pkg, :url => package_definition_delete_path(:repo_id => repo.getId(), :package_id => pkg.getId()), :html => {:id => "delete_package_"+pkg.getId, :method => :delete} do |f| %>

                                        <% if scope[:package_to_pipeline_map].get(pkg.getId()) == nil %>
                                            <%= md5_field %>
                                            <span id="package_delete_from_tree_<%= pkg.getId() %>">
                                        <button title='Delete this package.' type="button" class="remove" id="delete_button_from_tree_<%= pkg.getId() %>"></button>
                                                <%= render :partial => 'admin/shared/submit_with_prompt.html',
                                                           :locals => {:scope => {:button_container => "package_delete_from_tree_#{pkg.getId()}", :button_id => "delete_button_from_tree_#{pkg.getId()}", :message => "You are about to delete package #{pkg.getName()}", :on_confirmation_js => "$('#{"delete_package_#{pkg.getId()}"}').submit()"}} %>
                                    </span>
                                        <% else %>
                                            <button title='This package is being used in one or more pipeline(s), cannot delete the package' type="button" class="remove" id="delete_button_<%= pkg.getId() %>" disabled="disabled"></button>
                                        <% end %>
                                    <% end %>
                                </div>
                            </li>
                        <% end %>
                    <% end %>
                </ul>
            </li>
            <%- isCurrent = false %>
        <% end %>
    </ul>
<%- end -%>

<script type="text/javascript">
    Util.on_load(function(){
        new CustomTreeView().init();
        new CustomTreeView().bindTreeSearch(jQuery('.search'));
    })
</script>
